import { getToken, setToken, removeToken } from '@/utils/auth'
import { login } from '@/api/user'
const state = {
  token: getToken(),
}
const mutations = {
  setToken(state, token) {
    state.token = token
    // 同步到缓存
    setToken(token)
  },
  removeToken(){
    removeToken();
  }
}
const actions = {
  async login(context, data) {
    console.log(data)
    const res = await login(data)
    if (res.code === 200) {
      context.commit('setToken', res.data.token)
      return res.code
    } else {
      return res.code
    }
  },
  logOut(context) {
    context.commit('removeToken')
  },
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
